/**
 * 公共枚举类型定义
 * 用于统一管理各模块中的选项数据
 */

// 选项数据接口
export interface OptionItem {
  name: string;
  value: string;
}

// 金额类型选项
export const amountTypeOptions: OptionItem[] = [
  { name: "收入", value: "1" },
  { name: "支出", value: "2" },
  { name: "无金额", value: "3" },
];

// 合同期限类型选项
export const termTypeOptions: OptionItem[] = [
  { name: "固定期限", value: "1" },
  { name: "无固定期限", value: "2" },
];

// 签署方式选项
export const signMethodOptions: OptionItem[] = [
  { name: "纸质签署", value: "1" },
  { name: "电子签署", value: "2" },
];

// 印章类型选项
export const sealTypeOptions: OptionItem[] = [
  { name: "合同章", value: "1" },
  { name: "公章", value: "2" },
  { name: "法人章", value: "3" },
  { name: "财务章", value: "4" },
  { name: "人事章", value: "5" },
];

// 报销类型选项
export const expenseTypeOptions: OptionItem[] = [
  { name: "交通费", value: "1" },
  { name: "差旅费", value: "2" },
  { name: "招待费", value: "3" },
  { name: "经费", value: "4" },
];

// 支付方式选项
export const paymentMethodOptions: OptionItem[] = [
  { name: "银行转账", value: "bank_transfer" },
  { name: "现金支付", value: "cash" },
  { name: "支票支付", value: "check" },
  { name: "承兑汇票", value: "acceptance_bill" },
  { name: "信用证", value: "letter_of_credit" },
];

// 支付期限选项
export const paymentTermOptions: OptionItem[] = [
  { name: "即时付款", value: "immediate" },
  { name: "30天内", value: "30_days" },
  { name: "60天内", value: "60_days" },
  { name: "90天内", value: "90_days" },
  { name: "180天内", value: "180_days" },
  { name: "一年内", value: "1_year" },
];

// 报销发票类型选项
export const reimbursementTypeOptions: OptionItem[] = [
  { name: "其他发票", value: "other_invoice" },
  { name: "增值税专用发票", value: "vat_invoice" },
  { name: "增值税普通发票", value: "general_invoice" },
  { name: "收据", value: "receipt" },
  { name: "火车票", value: "train_ticket" },
  { name: "飞机票", value: "flight_ticket" },
];
// 委托方类型
export const clientTypeOptions: OptionItem[] = [
  { name: "自然人", value: "1" },
  { name: "法人", value: "2" },
];
/**
 * 根据值获取对应的名称
 * @param options 选项数组
 * @param value 值
 * @param defaultName 默认名称
 * @returns 对应的名称
 */
export const getOptionName = (
  options: OptionItem[],
  value: string | undefined,
  defaultName: string = '未知'
): string => {
  if (!value) return defaultName;
  const option = options.find(item => item.value === value);
  return option ? option.name : defaultName;
};

/**
 * 根据名称获取对应的值
 * @param options 选项数组
 * @param name 名称
 * @param defaultValue 默认值
 * @returns 对应的值
 */
export const getOptionValue = (
  options: OptionItem[],
  name: string | undefined,
  defaultValue: string = ''
): string => {
  if (!name) return defaultValue;
  const option = options.find(item => item.name === name);
  return option ? option.value : defaultValue;
};

// 便捷的获取名称方法
export const getAmountTypeName = (value: string | undefined) => 
  getOptionName(amountTypeOptions, value, '未知类型');

export const getTermTypeName = (value: string | undefined) => 
  getOptionName(termTypeOptions, value, '未知期限');

export const getSignMethodName = (value: string | undefined) => 
  getOptionName(signMethodOptions, value, '未知方式');

export const getSealTypeName = (value: string | undefined) => {
  if (!value) return '未知印章';
  
  try {
    // 尝试解析JSON字符串
    const sealTypes = JSON.parse(value);
    if (Array.isArray(sealTypes)) {
      // 如果是数组，获取每个印章类型的名称并用逗号连接
      return sealTypes
        .map(type => getOptionName(sealTypeOptions, type, ''))
        .filter(name => name) // 过滤掉空值
        .join('、') || '未知印章';
    }
  } catch (e) {
    // 如果解析失败，按照原来的方式处理单个值
    return getOptionName(sealTypeOptions, value, '未知印章');
  }
  
  return getOptionName(sealTypeOptions, value, '未知印章');
};

export const getExpenseTypeName = (value: string | undefined) => 
  getOptionName(expenseTypeOptions, value, '其他');

export const getPaymentMethodName = (value: string | undefined) => 
  getOptionName(paymentMethodOptions, value, '未知方式');

export const getPaymentTermName = (value: string | undefined) => 
  getOptionName(paymentTermOptions, value, '未知期限');

export const getReimbursementTypeName = (value: string | undefined) => 
  getOptionName(reimbursementTypeOptions, value, '其他发票');

  export const getclientTypeOptionsTypeName = (value: string | undefined) => 
  getOptionName(clientTypeOptions, value, '其他类型');
  